package com.ruoyi.tmsreceive.controller;

import com.aliyun.oss.OSSClient;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.tmsreceive.entity.Waybill;
import com.ruoyi.tmsreceive.service.CustomerCheckService;
import io.netty.handler.ssl.OpenSsl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

/**
 * Author: 邱兆颖  <br/>
 * Date: 2022/4/20-11:45  <br/>
 * Description:
 */
@RestController
@RequestMapping("/receive")
public class CustomerCheckController extends BaseController {
    @Resource
    private CustomerCheckService customerCheckService;
    @Autowired(required = false)
    private OSSClient ossClient;

    @GetMapping("/list")
    public TableDataInfo list(Waybill waybill) {
        startPage();
        List<Waybill> waybills = customerCheckService.selectWaybillList(waybill);
        return getDataTable(waybills);
    }

    /**
     * 获取货运单详细信息
     */
//    @RequiresPermissions("tmsreceive:waybill:query")
    @GetMapping(value = "/{waybillId}")
    public AjaxResult getInfo(@PathVariable("waybillId") Long waybillId) {
        return AjaxResult.success(customerCheckService.selectWaybillByWaybillId(waybillId));
    }

    /**
     * 修改货运单
     */
//    @RequiresPermissions("tmsreceive:waybill:edit")
    @Log(title = "货运单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Waybill waybill) {
        return toAjax(customerCheckService.updateWaybill(waybill));
    }

    /**
     * 新增投诉
     */
    @PostMapping("/complaint")
    public AjaxResult addComplaint(@RequestParam("file") MultipartFile file, @RequestParam("detail") String detail, @RequestParam("files") MultipartFile[] files) throws IOException {
            String originalFilename = file.getOriginalFilename();
            file.transferTo(new File("D://"+originalFilename));
            ossClient.putObject("qiuzhaoying",originalFilename,new FileInputStream("D://"+originalFilename));
            ossClient.shutdown();
//        if (files.length>0) {
//            for (MultipartFile multipartFile:files){
//                if (!multipartFile.isEmpty()) {
//                    String filename = multipartFile.getOriginalFilename();
//                    multipartFile.transferTo(new File("D://"+filename));
//                }
//            }
//        }
        return AjaxResult.success();
    }

    @GetMapping("/file")
    public void filetest() throws FileNotFoundException {
//        ossClient.putObject("qiuzhaoying", "file.jpg", new FileInputStream("D://test.jpg"));
//        ossClient.shutdown();
    }

//    @GetMapping("/list/{id}")
//    public AjaxResult getList(@PathVariable("id") Long id){
//        List<AWaybill> waybills = customerCheckService.selectWaybillList(id);
//        return AjaxResult.success(waybills);
//    }
}
